package com.study.design.model.behaviorPattern.iterator;

import java.util.ArrayList;
import java.util.List;

/**
* @Name: ConcreteMyAggregate
* @Description: 自定义聚合类
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-2-15 下午4:08:29 
* @Version: V1.0
 */
public class ConcreteMyAggregate {
	
	private List<Object> list = new ArrayList<Object>() ;
	
	public void addElement(Object object) {
		this.list.add(object) ;
	}
	
	public void removeElement(Object object) {
		this.list.remove(object) ;
	}

	public List<Object> getList() {
		return list;
	}

	public void setList(List<Object> list) {
		this.list = list;
	}
	
	/**
	* @Name: createIterator
	* @Description: 获取集合迭代器对象
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-15 下午4:14:33
	* @Parameters: @return
	* @Return: MyIterator
	 */
	public MyIterator createIterator() {
		return new ConcreteIterator() ;
	}
	
	/**
	* @Name: ConcreteIterator
	* @Description: 集合迭代器内部类
	* @Author: 李超	lc_jack@foxmail.com
	* @CreateDate: 2017-2-15 下午4:14:54 
	* @Version: V1.0
	 */
	private class ConcreteIterator implements MyIterator {
		
		//定义游标，用于记录遍历时的位置
		private int cursor ; 

		@Override
		public void first() {
			cursor = 0 ;
		}

		@Override
		public void next() {
			if(cursor < list.size()) {
				cursor ++ ;
			}
		}

		@Override
		public boolean hasNext() {
			if (cursor < list.size()) {
				return true ;
			}
			return false;
		}

		@Override
		public boolean isFirst() {
			return cursor == 0 ? true : false;
		}

		@Override
		public boolean isLast() {
			return cursor == (list.size() -1) ? true : false ;
		}

		@Override
		public Object getCurrentObject() {
			return list.get(cursor);
		}
		
	}
}
